#-*- coding: utf-8 -*-
import json
import os
import random

from django.conf import settings
from django.db import transaction

from account.models import *

def run():
    with open(os.path.join(settings.BASE_DIR, 'image_data.json')) as f:
        json_data = f.read()
    json_data = json.loads(json_data)

    data = []
    
    for i in json_data:
        label_name = i['label_name']
        key_list = i['key_list']
        label = Label.objects.filter(name=label_name).first()
        if not label:
            label = Label.objects.create(name=label_name)
        
        for key in key_list:
            data.append({
                'label': label,
                'key': key
            })

    random.shuffle(data)
    
    with transaction.atomic():
        for i in data:
            label, key = i['label'], i['key']
            Image.objects.create(label=label, key=key)

    